//
//  NHFileCacheManager.m
//  LZNeiHan
//
//  Created by 梁梓烽 on 16/10/9.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "NHFileCacheManager.h"
#import "NSFileManager+Paths.h"

@implementation NHFileCacheManager

/** 把对象归档到沙盒cache路径下 */
+(BOOL)saveObject:(id)object  byFileName:(NSString *)fileName
{
   NSString *path = [self appendFilePath:fileName];
   NSString *cachesPath = [path stringByAppendingString:@".archive"];
  return   [NSKeyedArchiver archiveRootObject:object toFile:cachesPath];
}
/** 通过文件名在沙盒中找到归档的对象 */
+(id)getObjectByFileName:(NSString *)fileName
{
    
    NSString *path = [self appendFilePath:fileName];
    NSString *cachesPath = [path stringByAppendingString:@".archive"];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:cachesPath];
}
///** 通过文件名删除沙盒中的归档对象 */
+(BOOL)removeObjectByFileName:(NSString *)fileName
{
    NSString *path = [self appendFilePath:fileName];
    NSString *cachesPath = [path stringByAppendingString:@".archive"];
    return [[NSFileManager defaultManager]removeItemAtPath:cachesPath error:nil];
}
//
///** 存储用户偏好设置奥 nsuserdefaults */
+(void)savaUserData:(id)object Forkey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults]setObject:object forKey:key];
    [[NSUserDefaults standardUserDefaults]synchronize];
}
///** 读取用户偏好设置 */
+(instancetype)readUserDataForKey:(NSString *)key
{
   return  [[NSUserDefaults standardUserDefaults]objectForKey:key];
}
///** 移除用户偏好设置 */
+(void)removeUserDataForKey:(NSString *)key
{
    
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:key];
}


+(NSString *)appendFilePath:(NSString *)fileName
{
    NSString *cachesPath = [NSFileManager cachesPath];
  
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",cachesPath,fileName];
    
    if (![[NSFileManager defaultManager]fileExistsAtPath:filePath isDirectory:nil]) {
        [[NSFileManager defaultManager]createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];
    }
    
    return filePath;
}

@end
